|
|
|
|
|
|
|
case, the SelText property would contain just the phrase "jumped over the lazy dog." The SelLength property would contain the integer value 25, which is the length of that string. And the SelStart property would contain the integer value 20, which means the selected phrase starts with the 20th character in the text box. |
|
|
|
|
|
|
|
|
Figure 19.9
Your users can copy text from one
text box and paste it into another. |
|
|
|
|
|
|
|
|
In addition to determining what has been selected, you can also set the properties from code to alter the selection. Every time you set the SelStart property, you must set the SelLength property to highlight some characters. To select the first three characters, you could use this code: |
|
|
|
|
|
|
|
|
txtTest.SelStart = 0
txtTest.SelLength = 3 |
|
|
|
|
|
|
|
|
The SelLength property can be changed multiple times. This causes the selection to increase or decrease in size, automatically updating the SelText property. Setting the SelText property from code causes the currently selected text to be replaced with a new stringfor example: |
|
|
|
|
|
|
|
|
txtTest.SelText = "jumped into oncoming traffic." |
|
|
|
|
|
|
|
|
One use of these properties is to highlight the entire contents of a text box. Suppose, for example, you have populated some text boxes for users to edit. When they press the Tab key to move to a text box, you might want to highlight whatever is in the text box automatically. This way, the user can start typing immediately, without deleting the existing text. This example is illustrated in the following code for a text box's GotFocus event: |
|
|
|
|
|
|
|
|
Private Sub txtSelect_GotFocus()
txtSelect.SelStart = 0
txtSelect.SelLength = Len(txtSelect.Text)
End Sub |
|
|
|
|
|
|
|
|
This same feature can be added by using the SendKeys statement to send the keystrokes for highlighting the entire text box: |
|
|
|
|
|
|
|
|
Private Sub txtSelect_GotFocus()
SendKeys ("{HOME}+{END}")
End Sub |
|
|
|
|
|
|
|
|
Working with Fonts and Colors |
|
|
|
|
|
|
|
|
Although drawings and pictures add a definite visual impact to your applications, the heart and soul of most of your programs will likely be the text fields for data entry and information display. Often, adding an image to enhance onscreen text is not possible or even useful. Instead, you must make sure that you use appropriate fonts and colors to get your point across. |
|
|
|
|
|